# -*- coding: utf-8 -*-
from web.controllers.api import route_api
from flask import request, jsonify
from application import app, db
import requests, json
from common.models.member.Member import Member
from common.models.device.CheckList import CheckList
from common.models.device.DeviceRelation import DeviceRelation
from common.models.device.DeviceOnly import DeviceOnly
from common.libs.Helper import getCurrentDate


@route_api.route("/consumer/addone", methods=["GET", "POST"])
def addone():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    data_all = content['data']
    app.logger.info(data_all)

    device_name = data_all['sbmc']
    device_model = data_all['xhgg']
    device_manufacture = data_all['zzc']
    device_rank = data_all['dj']
    device_no = data_all['ccbh']
    device_date = data_all['yxrq']

    module_device_only = DeviceOnly()
    module_device_only.device_name = device_name
    module_device_only.model = device_model
    module_device_only.manufacture = device_manufacture
    module_device_only.rank = device_rank
    module_device_only.number = device_no
    module_device_only.date = device_date
    module_device_only.member_id = member_id

    db.session.add(module_device_only)
    db.session.commit()

    return jsonify(resp)


@route_api.route("/consumer/delete", methods=["GET", "POST"])
def delete():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}

    content = request.get_json()
    usercode = content['usercode']
    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    data_all = content['data']           # data_all为一个数组
    app.logger.info(data_all)

    for data_item in data_all:
        device_id = data_item['sbid']    #获取将要删除的设备id

        #通过设备id和用户id去查找设备
        device_info = DeviceOnly.query.filter_by(member_id=member_id, id=device_id).first()

        if not device_info:
            resp['error'] = -2
            resp['msg'] = "没有找到相关联得设备"
            return jsonify(resp)

        db.session.delete(device_info)
        db.session.commit()

    return jsonify(resp)


@route_api.route("/consumer/list", methods=["GET", "POST"])
def list():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}

    content = request.get_json()
    usercode = content['usercode']
    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    # 通过member_id去DeviceOnly列表中找到所有此id得device
    device_info_array = DeviceOnly.query.filter_by(member_id=member_id).all()

    data = dict()
    device_info_list = []

    for device_item in device_info_array:
        device_info_list_item = dict()
        device_info_list_item['sbmc'] = device_item.device_name
        device_info_list_item['xhgg'] = device_item.model
        device_info_list_item['zzc'] = device_item.manufacture
        device_info_list_item['dj'] = device_item.rank
        device_info_list_item['ccbh'] = device_item.number
        device_info_list_item['yxrq'] = device_item.date
        device_info_list_item['sbid'] = device_item.id

        device_info_list.append(device_info_list_item)

    data['deviceInfo'] = device_info_list
    resp['data'] = data
    return jsonify(resp)


@route_api.route("/consumer/add", methods=["GET", "POST"])
def add():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}

    content = request.get_json()
    usercode = content['usercode']
    member_info = Member.query.filter_by(usercode=usercode).first()

    data_all = content['data']
    app.logger.info(data_all)

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    data_all = content['data']
    app.logger.info(data_all)

    company = data_all['sjdw']
    commit_date = data_all['wtrq']
    contact = data_all['lxr']
    phone = data_all['dh']

    device_list = data_all['deviceInfo']
    app.logger.info(device_list)
    device_num = len(device_list)

    if device_num < 1:
        resp['error'] = -2
        resp['msg'] = "没有设备信息"
        return jsonify(resp)

    model_checklist = CheckList()
    model_checklist.member_id = member_id
    model_checklist.device_number = device_num
    model_checklist.company = company
    model_checklist.commission_date = commit_date
    model_checklist.contact = contact
    model_checklist.mobile = phone
    model_checklist.member_status = "未受理"
    model_checklist.administor_status = "等待测试，请尽快分配测试"
    # model_checklist.test_status = "无"
    model_checklist.update_time = getCurrentDate()
    model_checklist.create_time = getCurrentDate()
    db.session.add(model_checklist)
    db.session.commit()

    for device_item in device_list:
        device_id = device_item['sbid']    # 获取设备id
        device_info_item = DeviceOnly.query.filter_by(id=device_id).first()  # 通过设备id得到设备信息

        model_device = DeviceRelation()

        model_device.check_id = model_checklist.id
        model_device.device_id = device_id
        # model_device.resault = "未检测"
        # model_device.testman_name  = "未分配"
        # model_device.test_status = "未开始"

        model_device.device_name = device_info_item.device_name
        model_device.model = device_info_item.model
        model_device.manufacture = device_info_item.manufacture
        model_device.rank = device_info_item.rank
        model_device.number = device_info_item.number
        model_device.date = device_info_item.date

        model_device.update_time = getCurrentDate()
        model_device.create_time = getCurrentDate()

        db.session.add(model_device)
        db.session.commit()

    return jsonify(resp)


@route_api.route("/consumer/commit_depread", methods=["GET", "POST"])
def consumer_commit():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}
    content = request.get_json()
    usercode = content['usercode']
    homeid = content['homeid']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    checklist_info = CheckList.query.filter_by(member_id=member_id, id=homeid).first()
    if not checklist_info:
        resp['error'] = -2
        resp['msg'] = "找不到此用户之前待提交列表"
        return jsonify(resp)

    checklist_info.member_status = "未受理"
    checklist_info.administor_status = "等待测试，请尽快分配测试"
    db.session.commit()

    return jsonify(resp)


@route_api.route("/consumer/detail", methods=["GET", "POST"])
def consumer_detail():
    resp = {'error': 200, 'msg': '操作成功', 'data': {}}

    content = request.get_json()
    usercode = content['usercode']
    check_id = content['homeid']

    member_info = Member.query.filter_by(usercode=usercode).first()

    if not member_info:
        resp['error'] = -1
        resp['msg'] = "无此用户"
        return jsonify(resp)

    member_id = member_info.id

    checklist_info = CheckList.query.filter_by(member_id=member_id, id=check_id).first()
    device_info_array = DeviceRelation.query.filter_by(check_id=check_id).all()

    data = dict()
    device_info_list = []

    data['sjdzt'] = checklist_info.member_status
    data['sjdw'] = checklist_info.company
    data['wtrq'] = checklist_info.commission_date
    data['lxr'] = checklist_info.contact
    data['dh'] = checklist_info.mobile
    # data['dhyy'] = checklist_info.reason

    for device_item in device_info_array:
        device_info_list_item = dict()

        device_info_list_item['sbmc'] = device_item.device_name
        device_info_list_item['xhgg'] = device_item.model
        device_info_list_item['zzc'] = device_item.manufacture
        device_info_list_item['dj'] = device_item.rank
        device_info_list_item['ccbh'] = device_item.number
        device_info_list_item['yxrq'] = device_item.date
        device_info_list_item['jcjg'] = device_item.resault
        device_info_list_item['csry'] = device_item.testman_name
        device_info_list_item['sbid'] = device_item.device_id
        device_info_list_item['bhgyy'] = device_item.reason_back
        device_info_list_item['dhyy'] = device_item.reason_force
        # app.logger.info(device_info_list_item)
        device_info_list.append(device_info_list_item)
        app.logger.info(device_info_list)

    data['deviceInfo'] = device_info_list

    resp['data'] = data
    return jsonify(resp)
















